/**
 * 
 */
package datafloweditor.engine.core.launching;

import java.text.MessageFormat;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMRunnerConfiguration;

/**
 * @author Michal Antolik
 *
 */
public class DFLaunchDelegate extends LaunchConfigurationDelegate {

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void launch(ILaunchConfiguration configuration, String mode,
			ILaunch launch, IProgressMonitor monitor) throws CoreException {
		
		// scheme name
		String scheme = configuration.getAttribute(IDFConstants.ATTR_DATAFLOW_SCHEME, (String)null);
		if (scheme == null) {
			abort("Dataflow scheme unspecified.", null);
		}
		
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(scheme));
		if (!file.exists()) {
			abort(MessageFormat.format("Dataflow scheme {0} does not exist.", new Object[] {file.getFullPath().toString()}), null);
		}
		
		IJavaProject project = JavaRuntime.getJavaProject(configuration);
		IVMInstall vmInstall = JavaRuntime.getVMInstall(project);
		   if (vmInstall == null)
		      vmInstall = JavaRuntime.getDefaultVMInstall();
		   if (vmInstall != null) {
		      IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
		      if (vmRunner != null) {
		         String[] classPath = null;
		         try {
		            classPath = JavaRuntime.computeDefaultRuntimeClassPath(project);
		         } catch (CoreException e) { }
		         if (classPath != null) {
		            VMRunnerConfiguration vmConfig = 
		               new VMRunnerConfiguration("Runner", classPath);
//		            ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
		            vmRunner.run(vmConfig, launch, monitor);
		         }
		      }
		   }

		
//		commandList.add(file.getLocation().toOSString());
//		
//		int requestPort = -1;
//		int eventPort = -1;
//		if (mode.equals(ILaunchManager.DEBUG_MODE)) {
//			requestPort = findFreePort();
//			eventPort = findFreePort();
//			if (requestPort == -1 || eventPort == -1) {
//				abort("Unable to find free port", null);
//			}
//			commandList.add("-debug");
//			commandList.add("" + requestPort);
//			commandList.add("" + eventPort);
//		}
//		
//		String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
//		Process process = DebugPlugin.exec(commandLine, null);
//		IProcess p = DebugPlugin.newProcess(launch, process, path);
//		if (mode.equals(ILaunchManager.DEBUG_MODE)) {
//			IDebugTarget target = new PDADebugTarget(launch, p, requestPort, eventPort);
//			launch.addDebugTarget(target);
//		}

	}

	/**
	 * Throws an exception with a new status containing the given
	 * message and optional exception.
	 * 
	 * @param message error message
	 * @param e underlying exception
	 * @throws CoreException
	 */
	private void abort(String message, Throwable e) throws CoreException {
		throw new CoreException(new Status(IStatus.ERROR, IDFConstants.ID_DATAFLOW_DEBUG_MODEL, 0, message, e));
	}
}
